package de.piratech.dasding.data.database.mongo.data;

import java.util.ArrayList;
import java.util.List;

import net.vz.mongodb.jackson.DBRef;
import de.piratech.dasding.data.Action;
import de.piratech.dasding.data.Event;
import de.piratech.dasding.data.database.mongo.AbstractMongoDB.COLLECTIONS;

public class MongoEvent extends AbstractMongoAction<Event> {

  private List<DBRef<MongoAction, String>> actionRefs = new ArrayList<DBRef<MongoAction, String>>();
  private DBRef<MongoEventCategory, String> categoryRef;

  public MongoEvent() {
    super(new Event());
  }

  public MongoEvent(Event pEvent) {
    super(pEvent);

    categoryRef = new DBRef<MongoEventCategory, String>(pEvent.getCategory()
        .getId(), COLLECTIONS.EVENTCATEGORY_COLLECTION.getName());

    if (pEvent.getActions() != null && pEvent.getActions().size() > 0) {
      for (Action action : pEvent.getActions()) {
        actionRefs.add(new DBRef<MongoAction, String>(action.getId(),
            COLLECTIONS.ACTION_COLLECTION.getName()));
      }
    }

  }

  public DBRef<MongoEventCategory, String> getCategoryRef() {
    return categoryRef;
  }

  public void setCategoryRef(DBRef<MongoEventCategory, String> categoryRef) {
    this.categoryRef = categoryRef;
    getDataObject().setCategory(categoryRef.fetch().getDataObject());
  }

  public List<DBRef<MongoAction, String>> getActionRefs() {
    return actionRefs;
  }

  public void setActionRefs(List<DBRef<MongoAction, String>> actionRefs) {
    this.actionRefs = actionRefs;
    for (DBRef<MongoAction, String> dbRef : actionRefs) {
      getDataObject().addAction(dbRef.fetch().getDataObject());
    }
  }
}
